﻿//------------------------------------------------------------------------------
//Author: Christoph van der Fecht
//CodePlex: http://www.codeplex.com/site/users/view/ChristophvdF
//
//Copyright(c) Christoph van der Fecht
//------------------------------------------------------------------------------
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace SerienNewsReader.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class OptionsViewModel : ViewModelBase
    {
        /// <summary>
        /// The <see cref="Series" /> property's name.
        /// </summary>
        public const string SeriesPropertyName = "Series";

        /// <summary>
        /// Series string
        /// </summary>
        private string series = default(string);

        /// <summary>
        /// Command to save the settings
        /// </summary>
        private RelayCommand saveSettingsCommand;
        
        /// <summary>
        /// Initializes a new instance of the OptionsViewModel class.
        /// </summary>
        public OptionsViewModel()
        {
            if (IsInDesignMode)
            {
                this.Series = "Title0;Title1;Title2;Title3";
            }
            else
            {
                this.LoadConfig();
            }
        }

        /// <summary>
        /// Triggers the saving of the settings
        /// </summary>
        private void TriggerSave()
        {
#if Desktop
            this.SaveConfigForDesktop();
#elif WP
            this.SaveConfigForPhone
#endif
        }

        /// <summary>
        /// Saves the settings into the config file
        /// </summary>
        private void SaveConfigForDesktop()
        {
            config.Default.Series = this.Series;
            config.Default.Save();
        }

        /// <summary>
        /// Loads the config
        /// </summary>
        private void LoadConfig()
        {
#if Desktop
            this.Series = config.Default.Series;
#elif WP
            //implement file for settings...
#endif
        }

        /// <summary>
        /// Cleans up
        /// </summary>
        public override void Cleanup()
        {
            this.series = string.Empty;
            this.saveSettingsCommand = null;

            base.Cleanup();
        }

        /// <summary>
        /// Gets or sets the string containing all series of the user
        /// </summary>
        public string Series
        {
            get
            {
                return series;
            }

            set
            {
                if (series == value)
                {
                    return;
                }
                series = value;
                RaisePropertyChanged(SeriesPropertyName);
            }
        }

        /// <summary>
        /// Gets the command to save the settings
        /// </summary>
        public RelayCommand SaveSettingsCommand
        {
            get
            {
                if (this.saveSettingsCommand == null)
                {
                    this.saveSettingsCommand = new RelayCommand(this.TriggerSave);
                }
                return this.saveSettingsCommand;
            }
        }
    }
}